package com.baijiayun.live.ui

import androidx.lifecycle.Observer
import androidx.constraintlayout.widget.ConstraintLayout
import android.view.View
import android.widget.FrameLayout
import com.baijiayun.live.ui.base.getViewModel
import com.baijiayun.live.ui.chat.ChatPadFragment
import com.baijiayun.live.ui.chat.ChatViewModel
import com.baijiayun.live.ui.mainvideopanel.MainVideoFragment
import com.baijiayun.live.ui.pptpanel.PPTFragment
import com.baijiayun.live.ui.speakpanel.SingleSpeakFragment
import com.baijiayun.live.ui.topmenu.TopMenuFragment
import com.baijiayun.liveuibase.utils.DisplayUtils
import kotlinx.android.synthetic.main.activity_live_room_pad_single.*
import kotlinx.android.synthetic.main.layout_chat_single.*

/**
 * 三分屏1v1模板
 * class_type=6
 */

class LiveRoomSingleActivity : LiveRoomTripleActivity() {
    private lateinit var chatViewModel: ChatViewModel

    override fun getContentResId() = R.layout.activity_live_room_pad_single
    override fun initView() {
        pptContainer.id = View.generateViewId()
        removeViewFromParent(pptContainer)
        toolboxLayer = findViewById(R.id.activity_live_toolbox)
        pptContainerViewGroup = findViewById(R.id.activity_live_room_pad_room_ppt_container)
        pptContainerViewGroup?.addView(pptContainer, FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
        supportFragmentManager.beginTransaction()
                .replace(R.id.activity_live_room_pad_room_top_container, TopMenuFragment.newInstance())
                .replace(R.id.activity_live_room_pad_room_main_video_container, MainVideoFragment.newInstance())
                .replace(R.id.activity_live_room_pad_room_interaction_container, ChatPadFragment.newInstance())
                .replace(pptContainer.id, PPTFragment.newInstance())
                .replace(R.id.activity_live_room_pad_room_speaker_video_container, SingleSpeakFragment.newInstance())
                .commitNowAllowingStateLoss()
        routerViewModel.isShowEyeCare.value = findViewById<View>(R.id.activity_class_eye_care_layer).visibility == View.VISIBLE
    }

    override fun changeLayoutParams() {
        if (!DisplayUtils.isPad(this)) {
            if (!isAspectRatioNormal(this)) {
                val layoutParams = findViewById<View>(R.id.activity_live_room_pad_background).layoutParams
                if (layoutParams is ConstraintLayout.LayoutParams) {
                    if (isAspectRatioLarge(this)) {
                        layoutParams.dimensionRatio = "W,16:9"
                    }
                }
            }
        }
    }

    override fun afterNavigateToMain() {
        super.afterNavigateToMain()
        chatViewModel = getViewModel { ChatViewModel(routerViewModel) }
        chatViewModel.forbidPrivateChat = true
        activity_live_room_pad_room_chat_container?.setOnClickListener {
            //打开聊天界面(仅手机)
            routerViewModel.action2Chat.value = true
            chatViewModel.redPointNumber.value = 0
        }
        //1v1 课程视频比例9:16
        val videoParams = findViewById<View>(R.id.activity_live_room_pad_room_main_video_container).layoutParams
        val speakParams = findViewById<View>(R.id.activity_live_room_pad_room_speaker_video_container).layoutParams
        videoParams.height = resources.getDimensionPixelSize(R.dimen.main_video_single_width) * 9 / 16
        speakParams.height = videoParams.height
    }

    override fun afterObserveSuccess() {
        super.afterObserveSuccess()
        chatViewModel.redPointNumber.observe(this, Observer {
            it?.let {
//                tv_red_tip?.visibility = if (it > 0) View.VISIBLE else View.GONE
                var chatName = getString(R.string.chat)
                if (it > 0) {
                    chatName += "(${it})"
                }
                tv_chat_name?.text = chatName
            }
        })
        routerViewModel.action2Chat.observe(this@LiveRoomSingleActivity, Observer {
            it?.let {
                if (it) {
                    activity_live_room_pad_room_interaction_container.visibility = View.VISIBLE
                } else {
                    activity_live_room_pad_room_interaction_container.visibility = View.GONE
                }
            }
        })
        routerViewModel.isShowEyeCare.observe(this@LiveRoomSingleActivity, Observer {
            it?.let {
                findViewById<View>(R.id.activity_class_eye_care_layer).visibility = if (it) View.VISIBLE else View.GONE
            }
        })
    }
}


